package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.mes.qm.dao.RepairmethodDao;
import cn.b.sky.mes.qm.model.Repairmethod;
import cn.b.sky.mes.qm.model.condition.RepairmethodCondition;
import cn.b.sky.mes.qm.service.RepairmethodService;

@Service("repairmethodService")
public class RepairmethodServiceImpl implements RepairmethodService {

  private static final Logger LOG = LoggerFactory.getLogger(RepairmethodServiceImpl.class);

  @Autowired
  private RepairmethodDao repairmethodDao;

  @Override
  public List<Repairmethod> getRepairmethodsByCondition(RepairmethodCondition condition) {
    if (condition == null) {
      condition = new RepairmethodCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("createDateTime");//默认创建时间
    }
    return repairmethodDao.selectRepairmethodByCondition(condition);
  }

  @Override
  public int getRepairmethodCountByCondition(RepairmethodCondition condition) {
    if (condition == null) {
      condition = new RepairmethodCondition();
    }

    return repairmethodDao.selectRepairmethodCountByCondition(condition);
  }

  @Override
  public Repairmethod getRepairmethodBySiteCodeAndRepairmethodCode(String siteCode, String repairmethodCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(repairmethodCode)) {
      return null;
    }

    return repairmethodDao.selectRepairmethodByPk(siteCode, repairmethodCode);
  }

  @Override
  public String removeRepairmethodBySiteCodeAndRepairmethodCode(String siteCode, String repairmethodCode) {
    String message = "";
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(repairmethodCode)) {
      return null;
    }
    try {
      repairmethodDao.deleteRepairmethodByPk(siteCode, repairmethodCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        // 外键删除已存在提示
        message = SystemConstant.DATABASE_OPERATION_FK;
      } else {
        message = SystemConstant.DWZ_CODE_ERROR_MESSAGE;
      }
    }
    return message;
  }

  @Override
  public void saveRepairmethod(Repairmethod repairmethod) {
    if (repairmethod != null && !StringUtils.isBlank(repairmethod.getRepairmethodCode())) {
      repairmethodDao.insertRepairmethod(repairmethod);
    }

  }

  @Override
  public void modifyRepairmethod(Repairmethod repairmethod) {
    if (repairmethod != null && !StringUtils.isBlank(repairmethod.getRepairmethodCode())) {
      repairmethodDao.updateRepairmethod(repairmethod);
    }

  }

  @Override
  public List<Repairmethod> getRepairmethodsForSuggest(RepairmethodCondition condition) {
    return repairmethodDao.getRepairmethodsForSuggest(condition);
  }

}
